package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.PageParams;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import sun.security.rsa.RSASignature;

import java.time.LocalDateTime;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(Employee employee) {
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());
        String newPassWord = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        lqw.eq(Employee::getPassword, newPassWord);
        lqw.eq(Employee::getStatus,1);
        return employeeMapper.selectOne(lqw);


    }

    @Override
    public void save(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public Page<Employee> findPage(PageParams pageParams) {
        Page<Employee> pageE = new Page<>(pageParams.getPage(), pageParams.getPageSize());
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.like(pageParams.getName() != null, Employee::getName, pageParams.getName());
        employeeMapper.selectPage(pageE, lqw);
        System.out.println("pageE.getRecords()");
        System.out.println(pageE.getRecords());
        System.out.println("======");
        System.out.println("employeeMapper.selectPage(pageE,lqw).getRecords()");
        System.out.println(employeeMapper.selectPage(pageE,lqw).getRecords());
        return pageE;

    }

    @Override
    public int update(Employee employee) {
        int rows = employeeMapper.updateById(employee);
        return rows;
    }

   /* @Override
    public int updateStatus(Employee employee, Long id) {
        employee.setUpdateUser(id);
        employee.setUpdateTime(LocalDateTime.now());
        int rows = employeeMapper.updateById(employee);
        return rows;
    }*/
}
